package com.boardgame.gwt.service.operate.action.platcard;

import com.boardgame.common.entity.ResponseInfo;
import com.boardgame.gwt.data.GwtGameBoard;
import com.boardgame.gwt.data.GwtPlayerInfo;
import com.boardgame.gwt.data.card.BaseCard;
import com.boardgame.gwt.service.operate.SyntaxRequest;
import com.boardgame.gwt.service.operate.action.Initial.InitialAction;
import lombok.Data;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 打牌得钱的行动
 * 2元
 */
@Data
public abstract class PlayCardAction extends InitialAction {
//    @Override
//    public Pattern getPattern() {
//        return Pattern.compile(INITIAL_Card_ACTION);
//    }
    public PlayCardAction(){
        super.setTemplateHtml("select_card");
        this.setTemplateControl("PlayCardAction");
    }

    @Override
    public ResponseInfo action(GwtGameBoard gwtGameBoard, SyntaxRequest syntaxRequest, Matcher matcher) {
        super.action(gwtGameBoard, syntaxRequest, matcher);
        //打牌的编号
        int code = Integer.parseInt(matcher.group(1));
        GwtPlayerInfo gwtPlayerInfo = this.gwtGameBoard.getCurrentPlayer();
        //判断是否有这张牌
        BaseCard card = gwtPlayerInfo.getListCardHand().stream().filter(baseCard -> {return baseCard.getCode() == code;}).findFirst().orElse(null);
        if (card==null){
            return ResponseInfo.Failed("手牌中没有这张牌!");
        }
        else {

            //检测颜色
            if (!this.getCardColor().equals(card.getColor())){
                return ResponseInfo.Failed("打出卡牌不符合要求!需要颜色:"+card.getColor());
            }
            //手牌删除这样
            gwtPlayerInfo.getListCardHand().remove(card);
            //进入弃牌
            gwtPlayerInfo.getListCardDiscard().add(card);
            //获得2元钱
            gwtPlayerInfo.setCoins(this.getGainCoin());
        }

        return ResponseInfo.Success(null);
    }


    /**
     * 获得钱
     * @return
     */
    protected abstract int getGainCoin();

    /**
     * 颜色
     * @return
     */
    protected abstract String getCardColor();
//    /**
//     * 获得的钱
//     */
//    private int gainCoin=2;
//    /**
//     * 卡牌颜色
//     */
//    private String cardColor;
}
